
import { mapShapeFn } from './registShape.js'
const initGl = (canvas) => {
  let gl
  try {
    gl = canvas.getContext('webgl') || canvas.getContext("experimental-webgl");
    gl.viewportWidth = canvas.width;
    gl.viewportHeight = canvas.height;
  } catch (e) {
  }
  if (!gl) {
    alert("Could not initialise WebGL, sorry :-(");
  }
  return gl
}

export const _webgl = (options = {}, props = {}) => {
  let {
    width = 800,
    height = 600,
    shapes
  } = options
  let canvas = document.createElement('canvas')
  canvas.width = width
  canvas.height = height
  const gl = initGl(canvas)
  if (gl) {
    shapes.forEach(t => {
      let fn = mapShapeFn(t.shape)
      fn && fn(gl, t, props)
    })
  }
  return canvas
}